//
//  CheckBox.m
//  LOU
//
//  Created by iMacOne on 15/11/12.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "CheckBox.h"
#import "ToolUtil.h"
#import "Size.h"

@implementation CheckBox
- (id)initWithParent:(UIView *)parentView point:(CGPoint)point callback:(ResponseCallback)callback text:(NSString *)text checked:(BOOL)checked
{
    self = [super init];
    self.callback = callback;
    self.checked = checked;
    float item_size = margin_10 * 2;
    CGSize size = ([ToolUtil isBlank:text] ? CGSizeMake(0, 0) : [text sizeWithAttributes:@{NSFontAttributeName : FONT(14)}]);
    self.aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, item_size, item_size)];
    _aImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"checkbox_%@.png", (self.checked ? @"select" : @"normal")]];
    [self addSubview:self.aImageView];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.aImageView.frame)+ margin_3 , 0, size.width, item_size)];
    _label.font = FONT(14);
    _label.textColor = [UIColor blackColor];
    _label.text = ([ToolUtil isBlank:text]?@"":text);
    [self addSubview:_label];
    
    self.frame = CGRectMake(point.x, point.y, CGRectGetMaxX(_label.frame), item_size);
    [self addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
    [parentView addSubview:self];
    return self;
}

- (void)doClick:(UIButton*)btn {
    self.checked = !self.checked;
    self.aImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"checkbox_%@.png", (self.checked ? @"select" : @"normal")]];
//    if (self.checked) {
//        self.imageView.image = [UIImage imageNamed:@"checkbox_select"];
//    }else {
//        self.imageView.image = [UIImage imageNamed:@"checkbox_normal"];
//    }
    
    
    
    if (nil != self.callback) {
        self.callback([NSMutableDictionary dictionaryWithDictionary:@{@"checked" : [NSNumber numberWithBool:self.checked]}]);
    }
    
    
}




@end
